package utils;

import java.lang.reflect.Method;

public class SystemPropertiesReflection {

    protected static final String GET_METHOD = "get";
    protected static final String SET_METHOD = "set";

    /**
     * This class cannot be instantiated
     */
    private SystemPropertiesReflection() {

    }

    /**
     * Get the value for the given key, and returns as a String
     *
     * @param key Key of the system property
     * @return value for the given key or null if: the key isn't found or an error occurred
     */
    public static String get(String key) {
        return getSystemPropertyValue(key,null);
    }

    /**
     * Get the value for the given key.
     *
     * @param key Key of the system property
     * @param def default value to return if the system property is not found
     * @return value for the given key or null if: the key isn't found or an error occurred
     */
    public static String get(String key, String def) {
        return getSystemPropertyValue(key,def);
    }

    /**
     * Get the system property value for the given key.
     * @param key Key of the system property
     * @param def default value to return if the system property is not found
     * @return value for the given key or null if: the key isn't found or an error occurred
     */
    protected static String getSystemPropertyValue(String key, String def) {
        String ret = null;

        try {
            // Get the class object
            Class systemProperties = Class.forName("android.os.SystemProperties");


            if (null == def) {
                // Retrieve the method
                Method get = systemProperties.getMethod(GET_METHOD, String.class);

                // Invoke the get method
                ret = (String) get.invoke(null, key);
            } else {
                // Retrieve the method
                Method get = systemProperties.getMethod(GET_METHOD, String.class, String.class);

                // Invoke the get method
                ret = (String) get.invoke(null, key, def);
            }

            if ((null != ret) && (ret.isEmpty())) {
                // In case of empty string returning null
                ret = null;
            }
        } catch (Exception e) {
            // We ignore any exception ad return null
        }
        return ret;
    }

    /**
     * Set a system property to the given value
     * @param key the key for the system property
     * @param val the value of the system property
     */
    public static void set( String key, String val )
    {
        try {
            // Get the class object
            Class systemProperties = Class.forName("android.os.SystemProperties");

            Method set = systemProperties.getMethod(SET_METHOD, String.class, String.class);

            // Invoke the get method
            set.invoke(null, key, val);

        } catch (Exception e) {
            // We ignore any exception ad return null
        }
    }

    // 需要系统签名
    //修订版本列表码
    static String ID = get("ro.build.id");
    //显示屏参数
    static String DISPLAY = get("ro.build.display.id");
    //整个产品的名称
    static String PRODUCT = get("ro.product.name");
    //设备参数
    static String DEVICE = get("ro.product.device");
    //主板
    static String BOARD = get("ro.product.board");
    //cpu指令集
    static String CPU_ABI = get("ro.product.cpu.abi");
    //cpu指令集2
    static String CPU_ABI2 = get("ro.product.cpu.abi2");
    //硬件制造商
    static String MANUFACTURER = get("ro.product.manufacturer");
    //系统定制商
    static String BRAND = get("ro.product.brand");
    //版本即最终用户可见的名称
    static String MODEL = get("ro.product.model");
    //系统启动程序版本号
    static String BOOTLOADER = get("ro.bootloader");
    //硬件名称
    static String HARDWARE = get("ro.hardware");
    //硬件序列号
    static String SERIAL = get("ro.serialno");
    //build的类型
    static String TYPE = get("ro.build.type");
    //描述build的标签,如未签名，debug等等。
    static String TAGS = get("ro.build.tags");
    //唯一识别码
    static String FINGERPRINT = get("ro.build.fingerprint");

    static long TIME = getLong("ro.build.date.utc") * 1000;
    static String USER = get("ro.build.user");
    static String HOST = get("ro.build.host");

    public static  long getLong(String property) {
        try {
            return Long.parseLong(get(property));
        } catch (NumberFormatException e) {
            return -1;
        }
    }

    public static String demo = "ID:"+ID+"\n"
            +"PRODUCT:"+PRODUCT+"\n"
            +"DEVICE:"+DEVICE+"\n"
            +"BOARD:"+BOARD+"\n"
            +"CPU_ABI:"+CPU_ABI+"\n"
            +"CPU_ABI2:"+CPU_ABI2+"\n"
            +"MANUFACTURER:"+MANUFACTURER+"\n"
            +"BRAND:"+BRAND+"\n"
            +"MODEL:"+MODEL+"\n"
            +"BOOTLOADER:"+BOOTLOADER+"\n"
            +"HARDWARE:"+HARDWARE+"\n"
            +"SERIAL:"+SERIAL+"\n"
            +"TYPE:"+TYPE+"\n"
            +"TAGS:"+TAGS+"\n"
            +"FINGERPRINT:"+FINGERPRINT+"\n"
            +"TIME:"+TIME+"\n"
            +"USER:"+USER+"\n"
            +"HOST:"+HOST+"\n"
            +"DISPLAY:"+DISPLAY+"\n";
}
